//
//  ViewController.m
//  AnimationDemo
//
//  Created by 马滕亚 on 2020/6/9.
//  Copyright © 2020 M.Y.A. All rights reserved.
//

#import "ViewController.h"

#define angleToRadius(angle) ((angle) / 180.0 * M_PI)


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *likeButton;

@property (weak, nonatomic) IBOutlet UIImageView *giftImgView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/**  */
@property (nonatomic, strong) NSArray *imagesArr;
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) CAEmitterLayer *emitterLayer;


@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.index = 0;
    self.imagesArr = @[@"head1",@"head2",@"head3",@"head4"];
    self.imageView.image = [UIImage imageNamed:self.imagesArr[0]];
    
    [self.likeButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    [self.likeButton setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateSelected];

    [self exploition];
    
    NSArray *array = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
    
    NSMutableArray *newArray = [array mutableCopy];
    
    for (int i = 0; i<newArray.count; i++) {
        NSString *str = newArray[i];
        for (int j = i+1; j<newArray.count; j++) {
            NSString *obj = array[j];
            if (str == obj) {
                [newArray removeObject:obj];
            }
        }
        
    }
    
    NSLog(@"--%@",newArray);
    NSLog(@"--%@",array);

    
}

- (void)buttonClick {
    if (self.myDelegate && [self.myDelegate respondsToSelector:@selector(doSomething)]) {
        [self.myDelegate doSomething];
    }
    
    NSArray *arr = @[];
    
    
}

// 给点赞效果添加
- (void)exploition {
    self.emitterLayer = [CAEmitterLayer layer];
//    // 设置颗粒数
//    [self.emitterLayer setValue:@1000 forKeyPath:@"emitterCells.explosionCell.birthRate"];
//    // 开始动画
//    self.emitterLayer.beginTime = CACurrentMediaTime();
    CAEmitterCell *cell = [[CAEmitterCell alloc] init];
    cell.name = @"explosionCell";
    cell.lifetime = .8f;
    cell.birthRate = 4000; // 设置颗粒数
    cell.velocity = 50; // 中间值
    cell.velocityRange = 20; // (50 + - 15)
    cell.scale = 0.03f;
    cell.scaleRange = 0.02f;
    cell.contents = (id)[UIImage imageNamed:@"dot"].CGImage;
    
    // 设置到图片上
    // 设置粒子的系统大小、位置、方向
    self.emitterLayer.name = @"explosionLayer";
    // 设置形状为圆形
    self.emitterLayer.emitterShape = kCAEmitterLayerCircle;
    // 设置模式
    self.emitterLayer.emitterMode = kCAEmitterLayerOutline;
    // 设置大小
    self.emitterLayer.emitterSize = CGSizeMake(150, 150);
    // 添加到layer上
    self.emitterLayer.emitterCells = @[cell];
    // 数量
    self.emitterLayer.birthRate = 0;
    // 设置位置
    self.emitterLayer.position = CGPointMake(CGRectGetWidth(_likeButton.bounds)/2, CGRectGetHeight(_likeButton.bounds)/2);
    
    [self.likeButton.layer addSublayer:self.emitterLayer];
    
}

- (void)start {
    self.emitterLayer.beginTime = CACurrentMediaTime();
    self.emitterLayer.birthRate = 1;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.emitterLayer.birthRate = 0;
    });
}

- (IBAction)likeBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    // 关键帧动画 点赞效果变大
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    if (sender.selected) {
        animation.values = @[@2,@0.8,@1,@1.2,@1];
        animation.duration = 0.6f;
        [self start];
        
    } else {
        animation.values = @[@0.8,@1];
        animation.duration = 0.4f;
    }
    
    [self.likeButton.layer addAnimation:animation forKey:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self curveAnimation];
    
//    [self giftAnimation];
    
//    [self transformAnimation];
    
    [self transformGroup];
}

// 动画组
- (void)transformGroup {
    // 绘制运动曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 200)];
    [path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(100, 100) controlPoint2:CGPointMake(200, 300)];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor = UIColor.redColor.CGColor;
    [self.view.layer addSublayer:shapeLayer];
    
    CALayer *colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(0, 0, 60, 60);
    colorLayer.position = CGPointMake(50, 200);
    colorLayer.backgroundColor = UIColor.yellowColor.CGColor;
    [self.view.layer addSublayer:colorLayer];
    
    // 过山车动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    // 动画完 不回到原点
//    animation.duration = 4.0f;
//    animation.removedOnCompletion = NO;
//    animation.fillMode = kCAFillModeForwards;
//    [colorLayer addAnimation:animation forKey:nil];
    
    // 改变大小
    CABasicAnimation *sizeAnimation = [CABasicAnimation animation];
    sizeAnimation.keyPath = @"transform.scale";
    sizeAnimation.toValue = @0.5f;
//    sizeAnimation.duration = 4.0f;
//    [colorLayer addAnimation:sizeAnimation forKey:nil];
    
    // 修改颜色
    CABasicAnimation *colorAnimation = [CABasicAnimation animation];
    colorAnimation.keyPath = @"backgroundColor";
    colorAnimation.toValue = (id) UIColor.greenColor.CGColor;

//    colorAnimation.duration = 4.0f;
//    [colorLayer addAnimation:colorAnimation forKey:nil];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[colorAnimation,animation,sizeAnimation];
    // 相同的属性 可以放在动画组里
    group.duration = 4.0f;
    // 动画完 不回到原点
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    
    [colorLayer addAnimation:group forKey:nil];    
}


// 转场动画
- (void)transformAnimation {
    self.index++;
    if (self.index == 3) {
        self.index = 0;
    }
    NSString *imageName = self.imagesArr[self.index];
    self.imageView.image = [UIImage imageNamed:imageName];
    
    // 添加转场动画
    CATransition *animation = [CATransition animation];
    animation.type = @"pageCurl";
    animation.duration = 0.5;
    [self.imageView.layer addAnimation:animation forKey:nil];
}

// 类似删除应用的动画效果
- (void)giftAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.rotation";
    
//    // 第一种方式 设置完整的运动轨迹
//    animation.values = @[@angleToRadius(-8),@angleToRadius(8),@angleToRadius(-8)];

    
    // 第二种方式
    // 添加抖动范围
    animation.values = @[@angleToRadius(-8),@angleToRadius(8)];
    // 设置自动反转动画
    animation.autoreverses = YES;
    // 两倍速度
    animation.speed = 2;
    
    // 设置重复次数
    animation.repeatCount = MAXFLOAT;
    [self.giftImgView.layer addAnimation:animation forKey:nil];
}

// 小车曲线运动
- (void)curveAnimation {
    // 绘制运动曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 200)];
    [path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(100, 100) controlPoint2:CGPointMake(200, 300)];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor = UIColor.greenColor.CGColor;
    [self.view.layer addSublayer:shapeLayer];
    
    // 小车
    CALayer *carLayer = [CALayer layer];
    carLayer.frame = CGRectMake(0, 200-10, 20, 20);
    carLayer.contents = (id)[UIImage imageNamed:@"car.png"].CGImage;
    // 调整小车的位置 (通过锚点)
    carLayer.anchorPoint = CGPointMake(0.5, 0.8);
    [self.view.layer addSublayer:carLayer];
    
    // 动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.path = path.CGPath;
    animation.duration = 4.0f;
    animation.rotationMode = kCAAnimationRotateAuto;
    // 动画完 不回到原点
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [carLayer addAnimation:animation forKey:nil];
}



@end
